package org.example.login.Impl;

import com.alibaba.fastjson.JSONObject;
import org.example.feign.feignClient.PojoServiceClient;
import org.example.feign.pojo.*;
import org.example.login.Strategy.ConcreteStrategyA;
import org.example.login.Strategy.ConcreteStrategyB;
import org.example.login.Strategy.ContextU;
import org.example.login.Strategy.Strategy;
import org.example.login.service.WxSessionService;
import org.springframework.stereotype.Service;

@Service
public class WxSessionServiceImpl implements WxSessionService {

    private final PojoServiceClient pojoServiceClient;

    public WxSessionServiceImpl(PojoServiceClient pojoServiceClient) {
        this.pojoServiceClient = pojoServiceClient;
    }


    public int getID(UserInfo userInfo, UserInfo1 userInfo1){
        int id=0;
        if (userInfo != null){
            id=2;
        }else  if (userInfo1 !=null){
             id=4;
        }

        return id;

    }
    public Customer inCustomer(UserInfo userInfo, String code, UserInfo1 userInfo1) {
        ContextU c=new ContextU();
         Customer customer=null;
        if (userInfo != null) {
            Strategy s = new ConcreteStrategyA();
            c.setStrategy(s);
            c.strategyMethod(userInfo, code,userInfo1);
           customer=s.getCustomer();
        } else {
            // 处理 userInfo 为 null 的情况
        }

        if (userInfo1 !=null){
            Strategy s = new ConcreteStrategyB();
            c.setStrategy(s);
            c.strategyMethod(userInfo, code,userInfo1);
            customer=s.getCustomer();
            }
            else {
                // 处理 userInfo 为 null 的情况
            }


        return customer;
    }

    public int Updata(Customer customer, String openid,int id) {
        // 更新Customer对象的openid
        customer.setOpenid(openid);
        System.out.println(customer);
        Midpojo midpojo = new Midpojo();
        midpojo.setId(id);
        midpojo.setCustomer(customer);

      int uID=pojoServiceClient.Updata( midpojo);
        // 返回JsCode2SessionResponse对象
        return uID;

    }

    public JsCode2SessionResponse ReturnJsResponse(String openid, int uID, JSONObject jsonObject)   {
        // 返回JsCode2SessionResponse对象
        JsCode2SessionResponse sessionResponse = new JsCode2SessionResponse();
        sessionResponse.setOpenid(openid);
        sessionResponse.setuID(uID);
        sessionResponse.setSessionKey(jsonObject.getString("session_key"));
        System.out.println(sessionResponse.getOpenid());
        return sessionResponse;
    }



}
